package top.yihoxu.yoj.judge.codesandbox.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import org.apache.commons.lang3.StringUtils;
import top.yihoxu.yoj.common.ErrorCode;
import top.yihoxu.yoj.exception.BusinessException;
import top.yihoxu.yoj.judge.codesandbox.CodeSandbox;
import top.yihoxu.yoj.judge.codesandbox.model.ExecuteCodeRequest;
import top.yihoxu.yoj.judge.codesandbox.model.ExecuteCodeResponse;

/**
 * @Author dth
 * @Date 2024/5/3 21:06
 * @PackageName:top.yihoxu.yoj.judge.codesandbox.impl
 * @ClassName: ThirdPartyCodeSandBox
 * @Description: 远程代码沙箱
 * @Version 1.0
 */
public class RemoteCodeSandBox implements CodeSandbox {
    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
        String url="http://localhost:8123/executeCode";
        String json= JSONUtil.toJsonStr(executeCodeRequest);
        HttpResponse execute = HttpUtil.createPost(url).body(json).execute();
        String resultBody = execute.body();
        System.out.println(resultBody);
        if (StringUtils.isBlank(resultBody)){
            throw new BusinessException(ErrorCode.API_REQUEST_ERROR,"接口调用失败");
        }

        return JSONUtil.toBean(resultBody,ExecuteCodeResponse.class) ;


    }
}
